// 从中序与前序遍历序列构造二叉树
function buildTree(preorder, inorder) {
    if (!preorder.length) {
        return null
    }
    // 前序遍历可以拿到根节点的值
    let val = preorder.shift()
    // 中序遍历可以拿到根节点值的索引位置
    let index = inorder.indexOf(val)
    let root = new TreeNode(val)
    root.left = buildTree(preorder.slice(0, index), inorder.slice(0, index))
    root.right = buildTree(preorder.slice(index), inorder.slice(index + 1))
    return root
}

function TreeNode(val) {
    this.val = val
    this.left = null
    this.right = null
}

let preorder = [3,9,20,15,7]
let inorder = [9,3,15,20,7] 
console.log(buildTree(preorder, inorder))